package com.heima.common.aliyun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.6.0</version>
</dependency>
*/
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class ScanText {

    private String accessKeyId;
    private String secret;

    public Map greeTextScan(String content) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, secret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();


        request.setMethod(MethodType.POST);

        request.setDomain("imageaudit.cn-shanghai.aliyuncs.com");

        request.setVersion("2019-12-30");

        request.setAction("ScanText");


        request.putQueryParameter("Tasks.1.Content", content.trim());


        request.putQueryParameter("Labels.1.Label", "spam");
        request.putQueryParameter("Labels.2.Label", "porn");
        request.putQueryParameter("Labels.3.Label", "politics");
        request.putQueryParameter("Labels.4.Label", "abuse");
        request.putQueryParameter("Labels.5.Label", "terrorism");
        request.putQueryParameter("Labels.6.Label", "ad");
        request.putQueryParameter("Labels.7.Label", "contraband");
        request.putQueryParameter("Labels.8.Label", "flood");


        Map resultMap = new HashMap();
        Map map = new HashMap();

        JSONObject scrResponse = null;
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

            scrResponse = JSON.parseObject(new String(response.getHttpResponse().getHttpContent(), "UTF-8"));

            if (scrResponse != null) {
                resultMap.putAll(scrResponse);
                String requestId = resultMap.get("RequestId").toString();
                Map data = (Map) resultMap.get("Data");
                Object results = data.get("Elements");
                JSONArray results1 = (JSONArray) results;
                for (Object o : results1) {
                    Map imagOrSub = (Map) o;
                    String taskId = imagOrSub.get("TaskId").toString();
                    JSONArray subResults = (JSONArray) imagOrSub.get("Results");
                    for (Object subResult : subResults) {
                        Map result = (Map) subResult;
                        String suggestion = result.get("Suggestion").toString();
                        BigDecimal rate = (BigDecimal) result.get("Rate");
                        String label = result.get("Label").toString();

                        System.out.println("suggestion = [" + label + "]");
                        if (!"pass".equals(suggestion)) {
                            map.put("requestId", requestId);
                            map.put("taskId", taskId);
                            map.put("suggestion", suggestion);
                            map.put("label", label);
                            map.put("rate", rate);

                            return map;
                        }
                    }
                }
            }
            map.put("suggestion", "pass");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            /**
             * 表明请求整体处理失败，原因视具体的情况详细分析
             */
            System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
            return null;
        }
    }
}